package com.hnkywl.sts.service.cwgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cggl.CgdDao;
import com.hnkywl.sts.dao.cwgl.KhzhsfmxDao;
import com.hnkywl.sts.dao.cwgl.TxcdDao;
import com.hnkywl.sts.dao.htgl.HtDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.sys.ZdjDao;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.dao.zjgl.PjdrDao;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.cwgl.Khzhsfmx;
import com.hnkywl.sts.entity.cwgl.VoucherItemData;
import com.hnkywl.sts.entity.htgl.Ht;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.sys.Zdj;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.entity.zjgl.Pj;
import com.hnkywl.sts.entity.zjgl.Pjdr;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.zjgl.PjglService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-14
 * Time: 上午11:26
 * To change this template use File | Settings | File Templates.
 */
@Service
public class FxkzService {
    private XsdDao xsdDao;
    private ZdjDao zdjDao;
    /**
     * 风险控货值计算
     * 4.未执行部分货物应支付货款 = 控货单价*数量 W
     * 5.代理费 = （销售单含税价 - 采购单含税价） * 合同数量
     * 6 最低控货值 =未执行部分货物应支付货款 +代理费 + 客户欠款  Z
     * 7. （W-Z）/ Z > 5%  允许放货 其他情况不允许
     * @param xsdId  销售单ID
      * @return
     */

   public Map<String,String> FxkzzJs(Long xsdId){
       SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
       Map<String,String>  khz = new HashMap<String,String>();
       //未执行部分货物应支付货款
       Double wzxzfhk  = 0.0d;
       //代理费
       Double Dlf = 0.0d;
       //最低控货值
       Double zdkhz = 0.0d;
       //1找寻合同对应的采购单销售单
       Xsd xsd = xsdDao.get(xsdId);
       for(Xsmx xsmx : xsd.getXsmxList()){
           //当日指导价
           Zdj drZdj = zdjDao.findZdjByPmAndDrrq(xsmx.getJtwz().getPm().getId(), new Date());
           //合同签订当日指导价
           Zdj htZdj = zdjDao.findZdjByPmAndDrrq(xsmx.getJtwz().getPm().getId(), xsd.getXsht().getQdrq());
           if(drZdj==null){
               khz.put("status","0");
               khz.put("message",xsmx.getJtwz().getPm().getName()+"在"+dateformat.format(new Date())+"没有指导价");
               return  khz;
           }
           if(htZdj==null){
               khz.put("status","0");
               khz.put("message",xsmx.getJtwz().getPm().getName()+"在"+dateformat.format(xsd.getXsht().getQdrq())+"没有指导价");
               return  khz;
           }
           wzxzfhk  = wzxzfhk + (xsmx.getKh().getSl()*xsmx.getKh().getKhdj());
           Dlf += (xsmx.getHsj()- xsmx.getKh().getCgdmx().getHsj())*xsmx.getSbsl();
       }
       zdkhz = Dlf+wzxzfhk;
       if((wzxzfhk-zdkhz)/wzxzfhk<0.05){
           khz.put("status","1");
           khz.put("message","风险控货值小于5%，不许放货");
           return  khz;
       }else{
           khz.put("status","0");
           khz.put("message","OK");
           return  khz;
       }
   }


    @Autowired
    public void setXsdDao(XsdDao xsdDao) {
        this.xsdDao = xsdDao;
    }
    @Autowired
    public void setZdjDao(ZdjDao zdjDao) {
        this.zdjDao = zdjDao;
    }
}
